package com.musiklegal;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class InfoParser extends DefaultHandler 
{

	
	DataSong anItem;
	private boolean in_entry =false;
	private boolean in_song = false;
	private boolean in_artist = false;
	private boolean in_idring = false;
	private boolean in_album = false;
	private boolean in_alname = false;
	private boolean in_genre = false;
	private boolean in_label = false;
	private boolean in_albumid = false;
	private boolean in_track_id = false;
	private boolean in_artist_id=false;
	//private boolean in_filesize=false;
	private boolean in_api=false;
	private boolean in_comp=false;
	private boolean in_pub=false;
	private boolean in_year= false;
	private boolean in_preview = false;
	private boolean in_rating = false;
	
	
	
	
	
	
	

	private StringBuffer buffer;
	
	InfoParser()
	{
		anItem = new DataSong();
		buffer= new StringBuffer();
	}
	
	public void startDocument() throws SAXException 
	{
		
	}
	
	@Override
	public void endDocument() throws SAXException 
	{
		
	}

	@Override
	public void startElement(String namespaceURI, String localName,
			String qName, Attributes atts) throws SAXException {
		if(localName.equals("track_id"))
		{
			in_track_id=true;
		}
		else if(localName.equals("artist_id"))
		{
			in_artist_id=true;
		}
		else if(localName.equals("preview"))
		{
			in_preview = true;
			
		}
		else if (localName.equals("track_name")) {
				this.in_song = true;	
		}else if (localName.equals("artist")) {
				this.in_artist = true;	
		}else if (localName.equals("idring")) {
				this.in_idring = true;	
		}
		else if (localName.equals("merchant_api")) {
			this.in_api = true;	
		}else if (localName.equals("album_art")) {
				this.in_album = true;	
		}
		else if(localName.equalsIgnoreCase("album_id"))
		{
			in_albumid = true;
		}
		else if (localName.equals("album_name")) {
				this.in_alname = true;	
		}
		else if (localName.equals("composer")) {
			this.in_comp = true;	
		}else if (localName.equals("publisher")) {
			this.in_pub = true;	
		}else if (localName.equals("genre")) {
				this.in_genre = true;	
		}else if (localName.equals("label")) {
				this.in_label = true;	
		}
		else if(localName.equals("year"))
		{
			in_year=true;
		}
		else if(localName.equals("rating"))
		{
			in_rating =true;
		}
//		else if(localName.equals("filesize"))
//		{
//			in_filesize=true;
//		}
	}
	
	@Override
	public void endElement(String namespaceURI, String localName, String qName)
			throws SAXException {
		if(localName.equals("preview"))
		{
			anItem.preview = buffer.toString();
			buffer = new StringBuffer();
				this.in_preview = false;
		}
		
		else if(localName.equals("track_id"))
		{
			anItem.track_id = buffer.toString();
			buffer = new StringBuffer();
				this.in_track_id = false;
		}
		else if (localName.equals("track_name")) 
		{
			anItem.song = buffer.toString();
			buffer = new StringBuffer();
				this.in_song = false;	
		}else if (localName.equals("artist")) {
			
			anItem.artist = buffer.toString();
			buffer = new StringBuffer();
				this.in_artist = false;	
		}else if (localName.equals("idring")) {
			anItem.idring = buffer.toString();
			buffer = new StringBuffer();
				this.in_idring = false;	
		}
		else if(localName.equals("artist_id"))
		{
			anItem.artist_id = buffer.toString();
			buffer = new StringBuffer();
			in_artist_id =false;
		}
		else if (localName.equals("merchant_api")) {
			anItem.api = buffer.toString();
			buffer = new StringBuffer();
				this.in_api = false;	
		}else if (localName.equals("album_art")) {
			
			anItem.album = buffer.toString();
			buffer = new StringBuffer();
				this.in_album = false;	
		}
		else if(localName.equalsIgnoreCase("album_id"))
		{
			anItem.albid = buffer.toString();
			buffer = new StringBuffer();
				in_albumid= false;
		}
		else if(localName.equals("year"))
		{
			anItem.year = buffer.toString();
			buffer = new StringBuffer();
				in_year= false;
		}
		else if (localName.equals("album_name")) {
			anItem.alname = buffer.toString();
			buffer = new StringBuffer();
				this.in_alname = false;	
		}else if (localName.equals("composer")) {
			anItem.composer = buffer.toString();
			buffer = new StringBuffer();
				this.in_comp = false;	
		}else if (localName.equals("publisher")) {
			anItem.pub = buffer.toString();
			buffer = new StringBuffer();
			this.in_pub = false;
		}
		else if (localName.equals("genre")) {
			anItem.genre= buffer.toString();
			buffer = new StringBuffer();
				this.in_genre = false;	
				
		}else if (localName.equals("label")) {
			
			anItem.label = buffer.toString();
			buffer = new StringBuffer();
			this.in_label = false;	
		}	
		else if(localName.equals("rating"))
		{
			anItem.rating = buffer.toString();
			buffer = new StringBuffer();
			this.in_rating = false;	
		}
	}
	
	/** Gets be called on the following structure: 
	 * <tag>characters</tag> */
	@Override
    public void characters(char ch[], int start, int length) {
		
		if(in_track_id)
		{
			buffer.append(new String(ch,start,length));
		}
		if(in_year)
		{
			buffer.append(new String(ch,start,length));
		}
		if(this.in_song)
		{
			buffer.append(new String(ch,start,length));
    	}
		if(this.in_idring)
		{
			buffer.append(new String(ch,start,length));
    	}
		if(in_preview)
		{
			buffer.append(new String(ch,start,length));
		}
		if(in_rating)
		{
			buffer.append(new String(ch,start,length));
		}
		if(in_artist_id)
		{
			buffer.append(new String(ch,start,length));
		}
		if(this.in_api)
		{
			buffer.append(new String(ch,start,length));
    	}
		if(this.in_artist)
		{
			buffer.append(new String(ch,start,length));
    	}
		if(this.in_album)
		{
			buffer.append(new String(ch,start,length));
    	}
		if(this.in_genre)
		{
			buffer.append(new String(ch,start,length));
    	}
		if(this.in_label)
		{
			buffer.append(new String(ch,start,length));
    	}
		if(in_albumid)
		{
			buffer.append(new String(ch,start,length));
		}
		if(this.in_alname)
		{
			buffer.append(new String(ch,start,length));
    	}
		if(this.in_comp)
		{
			buffer.append(new String(ch,start,length));
    	}
		if(this.in_pub)
		{
			buffer.append(new String(ch,start,length));
    	}
		
    }
}
